package com.wl.controller;

import com.mysql.cj.util.StringUtils;
import com.wl.util.TokenUtils;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-06 18:30
 */
public abstract class BaseServlet extends HttpServlet {

//    protected String op;
//
//    @Override
//    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        op = request.getParameter("op");
//        if (StringUtils.isNullOrEmpty(op)) {
//            show404Common(request, response);
//            return;
//        }
//        super.service(request, response);
//    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void show404Common(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html><html lang=\"en\"><head></head><body>没有此操作</body></html>");
        out.flush();
        out.close();
    }

    /**
     * 检查用户是否登录
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    protected boolean checkToken(HttpServletRequest request, HttpServletResponse response) {
        String token = TokenUtils.parseTokenToken(request);
        if (StringUtils.isNullOrEmpty(token)) {
            return false;
        }
        return true;
    }

}
